﻿using Autofac;
using Autofac.Integration.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Windows.Forms;
using ZK.Common;

namespace ZK.YHP_TransManage
{
    static class Program
    {

        ///   应用程序的主入口点。
        /// </summary>
        public static ApplicationContext context;
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                //处理未捕获的异常   
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //处理UI线程异常   
                Application.ThreadException += Application_ThreadException;
                //处理非UI线程异常   
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
                //判断是否已经有需要运行一个实例，如果系统没有，则运行一个
                bool noRun;
                using (System.Threading.Mutex m = new System.Threading.Mutex(true, Application.ProductName + "杨伙盘", out noRun))
                {
                    if (noRun)
                    {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);

                        if (!Com.AppConfigsHelper.MyDebug)
                        {
                            Application.Run(new Login());//主要入口
                        }
                        else
                        {
                            UC.Test test = new UC.Test();
                            test.Show();
                            Application.Run(new Login());//主要入口
                        }
                    }
                    else
                    {
                        MessageBox.Show("目前已有一个程序在运行，请勿重复运行程序", "提示");
                    }
                }
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr(ex.ToString());
            }
        }

        //初始化等待处理函数
        private static void Application_Idle(object sender, EventArgs e)
        {
            //Application.Idle -= Application_Idle;
            //if (context.MainForm == null)
            //{
            //    var mw = new Login(); //主页面
            //    context.MainForm = mw;
            //    var sp = (StartP)context.Tag;
            //    Thread.Sleep(3000);
            //    sp.Close(); //关闭启动窗体 
            //    mw.Show(); //启动主程序窗体
            //}
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            DelegateHelper.InfoMessageEvent?.Invoke("系统出小差，请重新操作");
            Common.Log4.LogManage.WriteErr("非UI " + e.ExceptionObject.ToString());
        }

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            DelegateHelper.InfoMessageEvent?.Invoke("UI线程无响应，请重新点击");
            Common.Log4.LogManage.WriteErr("UI " + e.Exception.ToString());
        }



    }
}
